package com.smsc.headend.task.engine.po;

import lombok.Data;

import java.io.Serializable;

@Data
public class TouItem implements Serializable {
    int hour;
    int minute;
    int rate;
    int seasonId;
    int dayId;

    public TouItem(int h, int m, int r, int s, int d) {
        hour = h;
        minute = m;
        rate = r;
        seasonId = s;
        dayId = d;
    }

    public TouItem(byte buffer[], int offset) {
        int tmp = buffer[offset + 2];
        seasonId = (tmp % 0x10) - 1;
        dayId = tmp / 0x10;
        tmp = ((int) buffer[offset] & 0xFF) * 0x100 + ((int) buffer[offset + 1] & 0xFF);
        rate = tmp & 0x07;
        hour = (tmp >> 11) & 0x1F;
        minute = (tmp >> 5) & 0x3F;
    }

    public byte[] toBytes() {
        byte[] buffer = new byte[3];
        if (hour < 0 || 23 < hour || minute < 0 || 59 < minute || rate < 0 || 3 < rate) {
            return null;
        }

        int tmp = (hour * 64 + minute) << 5;
        tmp += rate;
        buffer[0] = (byte) (tmp / 0x100);
        buffer[1] = (byte) (tmp % 0x100);
        buffer[2] = (byte) (dayId * 0x10 + seasonId + 1);

        return buffer;
    }

    public String getTime() {
        return String.format("%02d:%02d:00", hour, minute);
    }

    public String getRate() {
        return Integer.toString(rate);
    }
}
